package com.eshop.modules.system.rest;

import org.springframework.web.bind.annotation.DeleteMapping;
import java.io.Serializable;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PutMapping;
import com.eshop.modules.aop.ForbidSubmit;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.PostMapping;
import com.eshop.exception.BadRequestException;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.validation.annotation.Validated;
import com.eshop.modules.system.domain.DictDetail;
import java.util.Map;
import java.util.HashMap;
import org.springframework.web.bind.annotation.GetMapping;
import io.swagger.annotations.ApiOperation;
import com.eshop.logging.aop.log.Log;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.data.domain.Sort;
import org.springframework.data.web.PageableDefault;
import org.springframework.data.domain.Pageable;
import com.eshop.modules.system.service.dto.DictDetailQueryCriteria;
import com.eshop.modules.system.service.DictDetailService;
import org.springframework.web.bind.annotation.RequestMapping;
import io.swagger.annotations.Api;
import org.springframework.web.bind.annotation.RestController;

@RestController
@Api(tags = { "系统：字典详情管理" })
@RequestMapping({ "/api/dictDetail" })
public class DictDetailController
{
    private final DictDetailService dictDetailService;
    private static final String ENTITY_NAME = "dictDetail";
    
    public DictDetailController(final DictDetailService dictDetailService) {
        this.dictDetailService = dictDetailService;
    }
    
    @Log("查询字典详情")
    @ApiOperation("查询字典详情")
    @GetMapping
    public ResponseEntity<Object> getDictDetails(final DictDetailQueryCriteria criteria, @PageableDefault(sort = { "sort" }, direction = Sort.Direction.ASC) final Pageable pageable) {
        return new ResponseEntity(this.dictDetailService.queryAll(criteria, pageable), HttpStatus.OK);
    }
    
    @Log("查询多个字典详情")
    @ApiOperation("查询多个字典详情")
    @GetMapping({ "/map" })
    public ResponseEntity<Object> getDictDetailMaps(final DictDetailQueryCriteria criteria, @PageableDefault(sort = { "sort" }, direction = Sort.Direction.ASC) final Pageable pageable) {
        final String[] names = criteria.getDictName().split(",");
        final Map<String, Object> map = new HashMap<String, Object>(names.length);
        for (final String name : names) {
            criteria.setDictName(name);
            map.put(name, this.dictDetailService.queryAll(criteria, pageable).get("content"));
        }
        return new ResponseEntity(map, HttpStatus.OK);
    }
    
    @Log("新增字典详情")
    @ApiOperation("新增字典详情")
    @PostMapping
    @PreAuthorize("@el.check('admin','dict:add')")
    public ResponseEntity<Object> create(@Validated @RequestBody final DictDetail resources) {
        if (resources.getId() != null) {
            throw new BadRequestException("A new dictDetail cannot already have an ID");
        }
        resources.setDictId(resources.getDict().getId());
        return new ResponseEntity(this.dictDetailService.save(resources), HttpStatus.CREATED);
    }
    
    @ForbidSubmit
    @Log("修改字典详情")
    @ApiOperation("修改字典详情")
    @PutMapping
    @PreAuthorize("@el.check('admin','dict:edit')")
    public ResponseEntity<Object> update(@Validated @RequestBody final DictDetail resources) {
        this.dictDetailService.saveOrUpdate(resources);
        return new ResponseEntity(HttpStatus.NO_CONTENT);
    }
    
    @ForbidSubmit
    @Log("删除字典详情")
    @ApiOperation("删除字典详情")
    @DeleteMapping({ "/{id}" })
    @PreAuthorize("@el.check('admin','dict:del')")
    public ResponseEntity<Object> delete(@PathVariable final Long id) {
        this.dictDetailService.removeById((Serializable)id);
        return new ResponseEntity(HttpStatus.OK);
    }
}
